<?php

/**
 * @author      Nguyen Duc Thuan <me@ndthuan.com>
 * @copyright   Copyright (c) 2009 Nguyen Duc Thuan <me@ndthuan.com>
 * @version     $Id: String.php 12 2009-11-03 03:17:46Z thuan.nguyen $
 */

class Fete_String
{
    /**
     * Get excerpt from string
     * @param string $source
     * @param integer $maxChars
     * @return string
     */
    static public function getExcerpt($source, $maxChars)
    {
        if (strlen($source) <= $maxChars) {
            return $source;
        }

        $output = substr($content, 0, $maxChars);
        $lastSpacePos = strrpos($output, ' ');
        if ($lastSpacePos > -1) {
            $output = substr($output, 0, $lastSpacePos);
        }

        return $output;
    }

    /**
     * Get string of n characters from right side of the source string
     * @param string $string
     * @param integer $length
     * @return string
     */
    static public function right($string, $length)
    {
        return substr($string, -$length);
    }

    /**
     * Gets xx characters from left of string
     * @param string $string
     * @param integer $length
     * @return string
     */
    static public function left($string, $length)
    {
        return substr($string, 0, $length);
    }

    /**
     * Gets string between 2 strings from source string
     * @param string $firstString
     * @param string $secondString
     * @param string $source
     * @return string
     */
    static public function between($firstString, $secondString, $source)
    {
        $firstPos = strpos($source, $firstString) + strlen($firstString);
        $secondPos = strpos($source, $secondString);

        return substr($source, $firstPos, $secondPos - $firstPos);
    }

    /**
     * Generates random string
     * @param integer $outputLength output string length including prefix
     * @param string $prefix
     * @param string $source source characters to take
     * @return string
     */
    static public function genRandom($outputLength = 0, $prefix = '', $source = '')
    {
        $output = '';
        $prefixLength = strlen($prefix);

        if ($outputLength < $prefixLength) {
            $outputLength = $prefixLength;
        }

        if ('' === $source) {
            $source = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890';
        }

        $maxIndex = strlen($source) - 1;
        while (strlen($output) < $outputLength)
        {
            $output .= $source[rand(0, $maxIndex)];
        }

        return $output;
    }

    /**
     * Removes Vietnamese accent marks from Unicode string
     * @param string $str
     * @return string
     */
    static public function removeVnAccents($str)
    {
        $str = str_replace(array('á', 'à', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ',
            'ẵ', 'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ'), 'a', $str);
        $str = str_replace(array('é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể',
            'ễ', 'ệ'), 'e', $str);
        $str = str_replace(array('í', 'ì', 'ỉ', 'ĩ', 'ị'), 'i', $str);
        $str = str_replace(array('ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ',
            'ỗ', 'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ'), 'o', $str);
        $str = str_replace(array('ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự', 'ú', 'ù', 'ủ',
            'ũ', 'ụ'), 'u', $str);
        $str = str_replace(array('ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ'), 'y', $str);
        $str = str_replace(array('Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ằ', 'Ẳ',
            'Ẵ', 'Ặ', 'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ'), 'A', $str);
        $str = str_replace(array('É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ế', 'Ề', 'Ể',
            'Ễ', 'Ệ'), 'E', $str);
        $str = str_replace(array('Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị'), 'I', $str);
        $str = str_replace(array('Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ố', 'Ồ', 'Ổ',
            'Ỗ', 'Ộ', 'Ơ', 'Ớ', 'Ờ', 'Ở', 'Ỡ', 'Ợ'), 'O', $str);
        $str = str_replace(array('Ư', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự', 'Ú', 'Ù', 'Ủ',
            'Ũ', 'Ụ'), 'U', $str);
        $str = str_replace(array('Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ'), 'Y', $str);
        $str = str_replace(array('đ', 'Đ'), array('d', 'D'), $str);

        return $str;
    }
}